#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Each person has two parents and two alleles
typedef struct person
{
    struct person *parents[2]; // 父母指针数组，存储两个父母的指针
    char alleles[2];           // 存储两个等位基因（如 'A', 'B', 'O'）
} person;

const int GENERATIONS = 3;    // 定义家族树的代数
const int INDENT_LENGTH = 4;  // 定义打印家族树时的缩进长度

person *create_family(int generations); // 创建家族树的函数
void print_family(person *p, int generation); // 打印家族树的函数
void free_family(person *p); // 释放家族树内存的函数
char random_allele(); // 随机生成等位基因的函数

int main(void)
{
    // Seed random number generator
    srand(time(0));

    // Create a new family with three generations
    person *p = create_family(GENERATIONS);

    // Print family tree of blood types
    print_family(p, 0);

    // Free memory
    free_family(p);
}

// Create a new individual with `generations`
person *create_family(int generations)
{
    // Allocate memory for new person
    person *new_person = malloc(sizeof(person));
    if (new_person == NULL)
    {
        return NULL; // 如果内存分配失败，返回 NULL
    }

    // If there are still generations left to create
    if (generations > 1)
    {
        // Create two new parents for current person by recursively calling create_family
        new_person->parents[0] = create_family(generations - 1);
        new_person->parents[1] = create_family(generations - 1);

        // Randomly assign current person's alleles based on the alleles of their parents
        new_person->alleles[0] = new_person->parents[rand() % 2]->alleles[rand() % 2];
        new_person->alleles[1] = new_person->parents[rand() % 2]->alleles[rand() % 2];
    }
    else
    {
        // Set parent pointers to NULL
        new_person->parents[0] = NULL;
        new_person->parents[1] = NULL;

        // Randomly assign alleles
        new_person->alleles[0] = random_allele();
        new_person->alleles[1] = random_allele();
    }

    // Return newly created person
    return new_person;
}

// Free `p` and all ancestors of `p`.
void free_family(person *p)
{
    // Handle base case
    if (p == NULL)
    {
        return;
    }

    // Free parents recursively
    free_family(p->parents[0]);
    free_family(p->parents[1]);

    // Free child
    free(p);
}

// Print each family member and their alleles.
void print_family(person *p, int generation)
{
    // Handle base case
    if (p == NULL)
    {
        return;
    }

    // Print indentation
    for (int i = 0; i < generation * INDENT_LENGTH; i++)
    {
        printf(" ");
    }

    // Print person
    if (generation == 0)
    {
        printf("Child (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
    }
    else if (generation == 1)
    {
        printf("Parent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
    }
    else
    {
        // 打印曾祖父母或更高代数的祖先
        for (int i = 0; i < generation - 2; i++)
        {
            printf("Great-");
        }
        printf("Grandparent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
    }

    // Print parents of current generation
    print_family(p->parents[0], generation + 1);
    print_family(p->parents[1], generation + 1);
}

// Randomly chooses a blood type allele.
char random_allele()
{
    int r = rand() % 3; // 随机生成一个 0 到 2 的整数
    if (r == 0)
    {
        return 'A'; // 如果随机数为 0，返回 'A'
    }
    else if (r == 1)
    {
        return 'B'; // 如果随机数为 1，返回 'B'
    }
    else
    {
        return 'O'; // 如果随机数为 2，返回 'O'
    }
}
